<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
if(!$_SESSION['isfounder']){
	exi('无权访问！','error','core/account.siteinfo/');
}
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
$groupname=array(
		'0'=>'大众免费版',
		'1'=>'商业授权版',
		'2'=>'年费商业版',
		'3'=>'永久商业版',
);
$tabs=array(
	array('op'=>'get','title'=>'云服务参数'),
	array('op'=>'user','title'=>'注册云服务'),
	array('action'=>'/cloud.initialize/','title'=>'一键安装基础组件/插件'),
);
if($_RQ['op']=='get'){
	$cloud=core_setting('cloud');
	if($cloud){
		$bak=cloud_request('cloud.domain.set',array(),true);
		$sign=substr($bak,0,32);
		$bak=substr($bak,32);
		if($sign==md5($bak.$cloud['secret'])){
			$result=@unserialize(base64_decode($bak));
			if($result['type']=='success'){
				$cloud['group']=$result['message']['group'];
				$cloud['username']=$result['message']['username'];
				$cloud['bindfans']=$cloud['fans']['uniacid']?'已绑定':'未绑定';
				$cloud['iscloud']=$cloud['username']?1:0;
				core_setting('cloud',$cloud);
			}
		}
	}
	$return['item']=array_fields(array('cloudkey','secret','url','group','username','iscloud','bindfans'),$cloud);
	$groupid = $return['item']['group'] ? $return['item']['group'] : '0';
	$return['item']['group'] = $groupname[$groupid];
	$return['htmls']=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>$tabs,
		'desc'=>'设置云服务信息',
		'edit'=>array(
			array('type'=>'string','name'=>'group','read'=>true,'title'=>'会员组','help'=>'[知悉]：大众免费版无法设置：版权信息,H5场景独立域名,PC站,手机站,小游戏,小程序去版权等,--> <a style="color:red" target="_blank" href="http://s.jinyunweb.com/manage/index.php?plugin=core&action=fuwu.vip">移步升级</a>'),
			array('type'=>'string','name'=>'username','title'=>'云平台账号','read'=>true),
			array('type'=>'string','name'=>'url','title'=>'接口url'),
			array('type'=>'string','name'=>'cloudkey','title'=>'接口token'),
			array('type'=>'string','name'=>'secret','title'=>'接口密钥','help'=>'移步进云市场注册服务：<a style="color:red" target="_blank" href="https://s.jinyunweb.com/">https://s.jinyunweb.com/</a>。注册成功后登陆进入后台——站长中心——密钥管理——获取接口参数。'),
			array('type'=>'modal','plugin'=>'core','show'=>array('iscloud'=>1),'action'=>'/account.workerline/bindfans/','name'=>'bindfans','title'=>'绑定微信粉丝','btntitle'=>'点击绑定','help'=>'绑定微信粉丝,可获取少量积分'),
		),
	);
	set_html('header','common/header');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$cloud=core_setting('cloud');
	$cloud['url']=trim($post['url']);
	$cloud['cloudkey']=trim($post['cloudkey']);
	$cloud['secret']=trim($post['secret']);
	core_setting('cloud',$cloud);//先保存参数以便调用云接口函数，必要！
	//测试接口参数是否正常，同时上传授权域名
	$cloud_domain=trim($post['domain']);
	if(!$cloud_domain){
		$cloud_domain=$_SERVER['HTTP_HOST'];
	}
	$params=array(
		'domain'=>$cloud_domain,
		'table_pre'=>TABLE_PRE,
	);
	$res=cloud_request('cloud.domain.set',$params);
	$cloud['group']=$res['group'];
	$cloud['username']=$res['username'];
	core_setting('cloud',$cloud);
	$names=array('jy_h5game','jy_print','jy_xiu','jy_alisms','jy_alipay','jy_weishop','jy_diypage');
	$check=pdo_count('core_plugins',array('name'=>$names));
	if(!$check<6){
		exi('云服务参数配置成功！系统检测到您尚未安装基础功能插件和组件，点击确定前往安装免费的官方基础功能插件和模版组件！','','/cloud.initialize/');
	}
	exi('设置成功！');
}
if($_RQ['op']=='user'){
	//通过账号密码注册云服务
	if($_RQ['postdata']){
		$post=get_postdata();
		$params=array(
			'username'=>trim($post['username']),
			'password'=>trim($post['password']),
		);
		$cloud_url='http://api.jinyunweb.com/api/index.php?action=regsiter';
		$res=cfc('http')->ihttp_request($cloud_url,$params);
		$res=@json_decode($res['content'],true);
		if($res['type']!='success'){
			exi('注册云服务失败！'.$res['message'],'error');
		}
		$cloud=$res['message'];
		core_setting('cloud',$cloud);
		$check=pdo_count('core_plugins',array());
		if(!$check){
			exi('云服务参数配置成功！系统检测到您尚未安装基础功能插件和组件，点击确定前往安装免费的官方基础功能插件和模版组件！','','/cloud.initialize/');
		}
		exi('注册成功！','','get');
	}
	$return['item']=array();
	$return['htmls']=array(
		'type'=>'edit',
		'tabs'=>$tabs,
		'desc'=>'通过输入进云官网账号密码来注册云服务',
		'edit'=>array(
			array('type'=>'string','name'=>'username','title'=>'进云账号','help'=>'前往进云应用市场（<a style="color:red" target="_blank" href="https://s.jinyunweb.com/">https://s.jinyunweb.com/</a>）注册获得账号'),
			array('type'=>'password','name'=>'password','title'=>'进云密码','help'=>'填写登陆进云官网的密码'),
		),
		
	);
	exi($return);
}
?>